package com.example.droideep.common.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.LongSparseArray;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListAdapter;
import android.widget.Toast;

import droideep.library.widget.AutoHeightGridView;

/**
 * 展示只实现多选，并且不可选择超过{@link #MAX_CHECK_NUM}的个数
 *
 * @author goodev
 * @date 15/7/4
 */
public class CheckGridView extends AutoHeightGridView {
    private static final int MAX_CHECK_NUM = 3;

    public CheckGridView(Context context) {
        super(context);
    }

    public CheckGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CheckGridView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean performItemClick(View view, int position, long id) {
        boolean handle = false;
        if (getChoiceMode() != CHOICE_MODE_NONE) {

            boolean check = getCheckedItemPositions().get(position);
            /**
             * 如果当前点击的item还没有被选中,需要做判断
             * 1.当前已经被选中的item数量大于等于{@link MAX_CHECK_NUM}时，就不能再继续选择item
             * 2.如果小于{@link MAX_CHECK_NUM},可以继续选择
             */
            if (!check) {
                if (getCheckedItemCount() >= MAX_CHECK_NUM) {
                    Toast.makeText(getContext(), "您只能选择三个", Toast.LENGTH_SHORT).show();
                } else {
                    return super.performItemClick(view, position, id);
                }
                handle = true;
            } else {
                return super.performItemClick(view, position, id);
            }
        }
        return handle;
    }
}
